package com.cloudkinto.controller.serialmanage;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.serialmanage.SerialNumberManageService;
import com.cloudkinto.service.serialmanage.vo.ProhibitOrRemoveReq;
import com.cloudkinto.service.serialmanage.vo.UpdateStatusReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhujiale
 * @since 2022-09-20
 */
@RestController
@Api(tags = "序列号管理模块")
@RequestMapping("/api/serial/manage")
public class SerialNumberManageController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(SerialNumberManageController.class);

    @Autowired
    private SerialNumberManageService service;

    @UserSessionRequired
    @PostMapping("/batchUpdate")
    @ApiOperation(value = "批量禁止出库/解除限制", notes = "根据id查看详情")
    public Object batchUpdate(@RequestBody @Validated ProhibitOrRemoveReq req) {
        return service.batchUpdate(req);
    }

    @UserSessionRequired
    @PostMapping("/batchUpdateStatus")
    @ApiOperation(value = "更新在库状态", notes = "根据id查看详情")
    public SingleResult batchUpdateStatus(@RequestBody @Validated UpdateStatusReq req) {
        service.batchUpdateStatus(req, getCurrentUserId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/importInit")
    @ApiOperation(value = "导入初始化", notes = "发哦如初始化")
    public SingleResult importInit() {
        return service.importInit(getCompanyId());
    }

    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"), @ApiImplicitParam(name = "pageSize", value = "页面大小"), @ApiImplicitParam(name = "storageId", value = "所在仓库"), @ApiImplicitParam(name = "timeType", value = "时间类型 预报日期：predict 入库日期：in 出库日期：out"), @ApiImplicitParam(name = "startDate", value = "开始日期 ymd"), @ApiImplicitParam(name = "endDate", value = "结束日期 ymd"), @ApiImplicitParam(name = "serialNumber", value = "序列号"), @ApiImplicitParam(name = "sku", value = "sku"), @ApiImplicitParam(name = "orderInputNumber", value = "入库单号"), @ApiImplicitParam(name = "orderOutputNumber", value = "出库单号")})
    @UserSessionRequired
    public PageResult listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();
        Long userId = getCurrentUserId();
        System.out.println("companyId:" + companyId);
        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String pageSize = request.getParameter("pageSize");
        String storageId = request.getParameter("storageId");
        String timeType = request.getParameter("timeType");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String serialNumber = request.getParameter("serialNumber");
        String boxNumber = request.getParameter("boxNumber");
        String sku = request.getParameter("sku");
        String orderInputNumber = request.getParameter("orderInputNumber");
        String orderOutputNumber = request.getParameter("orderOutputNumber");

        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("storageId", storageId);
        map.put("timeType", timeType);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("serialNumber", serialNumber);
        map.put("boxNumber", boxNumber);
        map.put("sku", sku);
        map.put("orderInputNumber", orderInputNumber);
        map.put("orderOutputNumber", orderOutputNumber);
        map.put("companyId", companyId + "");
        map.put("userId", userId + "");
        return service.getListPage(map);
    }

    @UserSessionRequired
    @PostMapping("/importSerialFile")
    @ApiOperation(value = "导入序列号信息")
    public Object importSerialFile(MultipartFile file, @RequestHeader("orderInputId") Long orderInputId) throws FileNotFoundException {
        return service.importSerialFile(file, orderInputId, getCurrentUserId(), getCompanyId());
    }

    @UserSessionRequired
    @GetMapping("/exportExcel")
    @ApiOperation(value = "导出excel", notes = "导出excel")
    @ApiImplicitParams({@ApiImplicitParam(name = "storageId", value = "所在仓库"), @ApiImplicitParam(name = "timeType", value = "时间类型 预报日期：predict 入库日期：in 出库日期：out"), @ApiImplicitParam(name = "startDate", value = "开始日期 ymd"), @ApiImplicitParam(name = "endDate", value = "结束日期 ymd"), @ApiImplicitParam(name = "serialNumber", value = "序列号"), @ApiImplicitParam(name = "sku", value = "sku"), @ApiImplicitParam(name = "orderInputNumber", value = "入库单号"), @ApiImplicitParam(name = "orderOutputNumber", value = "出库单号")})
    public SingleResult exportExcel(HttpServletRequest request) throws IOException {
        Long companyId = getCompanyId();
        Long userId = getCurrentUserId();

        Map<String, String> map = new HashMap<>();
        String storageId = request.getParameter("storageId");
        String timeType = request.getParameter("timeType");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String serialNumber = request.getParameter("serialNumber");
        String sku = request.getParameter("sku");
        String orderInputNumber = request.getParameter("orderInputNumber");
        String orderOutputNumber = request.getParameter("orderOutputNumber");

        map.put("storageId", storageId);
        map.put("timeType", timeType);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("serialNumber", serialNumber);
        map.put("sku", sku);
        map.put("orderInputNumber", orderInputNumber);
        map.put("orderOutputNumber", orderOutputNumber);
        map.put("companyId", companyId + "");
        map.put("userId", userId + "");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = simpleDateFormat.format(new Date());
        String fileName = "序列号管理" + time + ".xlsx";
        return SingleResult.success(service.exportExcel(map, fileName));
    }

}

